export const useSubmit = (props) => {
  // 绑定表单项
  const temp = ref({});
  // 提交类型 @add 添加 @edit 编辑
  const submitType = ref("add");
  // 表单校验规则
  const rule = ref({});
  const { check } = useValidate();
  const submit = async () => {
    const vaildate = await check(rule.value, temp.value, true);
    if (vaildate !== true) {
      let msg = Object.values(vaildate);
      uni.$u.toast(msg[0]);
    } else {
      if (submitType.value == "add") {
        props.addApi(temp.value).then((res) => {
          if (res.code == 200) {
            uni.$sc.toast("添加成功").then(() => {
              props.addSuccess && props.addSuccess(res);
            });
          }
        });
      }
      if (submitType.value == "edit") {
        props.editApi(temp.value).then((res) => {
          if (res.code == 200) {
            uni.$sc.toast("编辑成功").then(() => {
              props.editSuccess && props.editSuccess(res);
            });
          }
        });
      }
    }
  };
  return {
    temp,
    submit,
    submitType,
    rule,
  };
};
